package com.jbinfo.qingcaowan.base.service;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.jbinfo.qingcaowan.base.dao.BaseNoticeDao;
import com.jbinfo.qingcaowan.base.dao.NoticeUserDAO;
import com.jbinfo.qingcaowan.base.domain.NoticeUserDomain;
import com.jbinfo.qingcaowan.base.dto.NoticeUserDTO;
import com.jbinfo.qingcaowan.common.BusinessException;
import com.jbinfo.qingcaowan.common.ClonePojoUtil;
import com.jbinfo.qingcaowan.common.ExceptionConstants.BusinessExceptionCode;

/**
 * 
* @ClassName: UserServiceImpl 
* @Description: TODO 通知公告
* @author lizhenwei
* @date 2016年7月13日 下午22:23:02
 */
@Service("baseNoticeService")
public class BaseNoticeServiceImpl implements BaseNoticeService {

	@Resource(name="baseNoticeDao")
	private BaseNoticeDao baseNoticeDao;
	
	@Resource(name="noticeUserDAO")
	private NoticeUserDAO noticeUserDAO;

	// 根据条件查询通知公告数
	@Override
	public Object getNoticeCnt(NoticeUserDTO noticeUserDTO) {
		
		Integer nNoticeCnt = Integer.valueOf(0);
		if (noticeUserDTO.getUserid() == null 
				|| noticeUserDTO.getUserid().isEmpty()) {
			throw new BusinessException(BusinessExceptionCode.param_error, "用户ID不能为空");
		}
		NoticeUserDomain noticeUserDomain = ClonePojoUtil.copyClassFromTo(noticeUserDTO, NoticeUserDomain.class);
		
		//baseNotice.setNoticeStatus("0"); // 查询“未读”状态
		
		//long nCnt = baseNoticeDao.findNoticeCnt(baseNotice);
		List<NoticeUserDomain> noticeUserList = noticeUserDAO.findNotReadCnt(noticeUserDomain);
		
		nNoticeCnt = Integer.valueOf(noticeUserList.size());
		return nNoticeCnt;
	}

	// 查询通知公告列表
	@Override
	public Object getNoticeList(NoticeUserDTO noticeUserDTO) {
		
		List<NoticeUserDTO> noticeList = new ArrayList<NoticeUserDTO>();
		
		if (noticeUserDTO.getUserid() == null 
				|| noticeUserDTO.getUserid().isEmpty()) {
			throw new BusinessException(BusinessExceptionCode.param_error, "用户ID不能为空");
		}
		
		NoticeUserDomain noticeUserDomain = ClonePojoUtil.copyClassFromTo(noticeUserDTO, NoticeUserDomain.class);
		
		//baseNotice.setNoticeStatus("0"); // 查询“未读”状态
		
		List<NoticeUserDTO> baseNoticeList = noticeUserDAO.findNotReadNoticeList(noticeUserDomain);
		
		if(baseNoticeList == null || baseNoticeList.isEmpty()){
			return null;
		}
		for (NoticeUserDTO notice: baseNoticeList) {
			
			if (notice != null) {
				
				NoticeUserDTO noticeDTO = ClonePojoUtil.copyClassFromTo(notice, NoticeUserDTO.class);
				
				// 处理strReadTime
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				// 判断阅读时间非空
				if (noticeDTO.getReadtime() != null) {
					String strReadTime = sdf.format(noticeDTO.getReadtime());
					noticeDTO.setStrReadTime(strReadTime);
				}
				
				noticeList.add(noticeDTO);
			}
			
		}
		
		return noticeList;
	}
	
	// 更新通知状态为“已读”
	@Override
	public Object updateNoticeStatus(NoticeUserDTO noticeUserDTO) {
		
		if (noticeUserDTO.getNoticeid() == null || noticeUserDTO.getNoticeid().isEmpty()) {
			throw new BusinessException(BusinessExceptionCode.param_error, "通知ID不能为空！");
		}
		
		NoticeUserDomain noticeUserDomain = ClonePojoUtil.copyClassFromTo(noticeUserDTO, NoticeUserDomain.class);
		
		noticeUserDAO.updateReadStatus(noticeUserDomain);
		
		return null;
	}
	
}
